package com.ceba.test.modules.admin.logicService;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import abc.data.DataRow;
import abc.data.DataTable;
import com.ceba.base.enums.SealTypeEnum;
import com.ceba.base.helper.LoggerHelper;
import com.ceba.base.utils.ImageHelper;
import com.ceba.test.base.config.LogicPathUtil;
import com.ceba.test.base.configs.CebaPathUtil;
import com.ceba.test.base.enums.*;
import com.ceba.test.base.utils.CebaFileManageUtil;
import com.ceba.test.modules.admin.entity.*;
import com.ceba.test.modules.admin.service.*;
import com.ceba.test.modules.storage.logicService.StorageFileCacheManage;
import org.json.JSONArray;
import org.json.JSONObject;

import com.ceba.test.base.language.ResourceUtils;
import com.ceba.test.modules.email.entity.MsgMail;

import abc.Result;
import abc.web.HttpContext;

import javax.servlet.http.HttpServletRequest;

public class AdminLogicService {

	private InterfaceSignService interfaceSignService;
	private LogService logService;
	private OrganizeService organizeService;
    private DirManagerService dirManagerService;
    private StampService stampService;
    private TemplateWordService templateWordService;
    private GReportMergeService gReportMergeService;
    private UpgradeService upgradeService;
    private WaterPageNumberService waterPageNumberService;


	
	//interfaceSignService 类
	
	public boolean deleteFtpSetting(String id) throws Exception{
		return interfaceSignService.deleteFtpSetting(id);
	}
	
	/**
	 * 增加ftp配置
	 * @param ftp
	 * @return
	 */
	public boolean addFtp(FTPObject ftp) throws Exception{
		return interfaceSignService.addFtp(ftp);
	}

	/**
	 * 更新ftp配置
	 * @param ftp
	 * @return
	 */
	public boolean updateFtp(FTPObject ftp) throws Exception{
		return interfaceSignService.updateFtp(ftp);
	}

	public JSONObject getFTPListSql(String orderBy, String key, Map<String, Object> dic) throws Exception{
		return interfaceSignService.getFTPListSql(orderBy,key,dic);
	}

	/**
	 * 增加一条签名规则：key模式
	 * @param iSign
	 * @return
	 */
	public boolean addIsign(ISign iSign) throws Exception{
		return interfaceSignService.addIsign(iSign);
	}
	
	
	/**
	 *
	 * @return
	 */
	public boolean addCusResouse(CustomResource cusrule) throws Exception{
		return interfaceSignService.addCusResouse(cusrule);
	}
	
	public boolean addQiFengRule(QfRule cusrule) throws Exception{
		cusrule.setStorageAddressId(StorageFileCacheManage.getSystemUseAddressId());
		return interfaceSignService.addQiFengRule(cusrule);
	}
	
	public boolean queryQiFengRuelByName(String name){
		return interfaceSignService.queryQiFengRuelByName(name);
	}

	public boolean updateQiFengRule(QfRule cusrule) throws Exception{
		cusrule.setStorageAddressId(StorageFileCacheManage.getSystemUseAddressId());
		return interfaceSignService. updateQiFengRule(cusrule);
	}

	public boolean queryQifengByNameAndId(String id,String name){
		return interfaceSignService.queryQifengByNameAndId(id,name);
	}
	public boolean updateCusResouse(CustomResource cusrule) throws Exception{
		return interfaceSignService. updateCusResouse(cusrule);
	}
	
	
	public CustomResource getByIdCusResouse(String cusruleId)  throws Exception{
		return interfaceSignService.getByIdCusResouse(cusruleId);
	}
	
	
	public QfRule getQiFengRuleById(String cusruleId)  throws Exception{
		QfRule qf=	interfaceSignService.getQiFengRuleById(cusruleId);
		qf.setFilePath(LogicPathUtil.getBrowserPath(qf.getStorageAddressId(),qf.getFilePath()));
		return qf ;
	}

	

	/**
	 * 更新一条签名规则：key模式下
	 * @param iSign
	 * @return
	 */
	public boolean updateISign(ISign iSign) throws Exception{
		return interfaceSignService.updateISign(iSign);
	}
	
	public boolean deleteISign(String id) throws Exception{
		return interfaceSignService.deleteISign(id);
	}
	
	public boolean deleteIsCus(String id) throws Exception{
		return interfaceSignService.deleteIsCus(id);
	}
	
	public boolean deleteqiFengRule(String id) throws Exception{
		return interfaceSignService.deleteqiFengRule(id);
	}
	
	public JSONObject getInterfaceSignListSql(String key, String orderBy, Map<String, Object> dic) throws Exception{
		return interfaceSignService.getInterfaceSignListSql(key, orderBy, dic);
	}
	
	public JSONObject getCusfaceListSql(String orderBy,String key,Map<String,Object> dic) {
		JSONObject json=interfaceSignService.getCusfaceListSql(key, orderBy,dic);
		getCusfaceListSqlG(json);
		return json;
	}

	public JSONObject getCusfaceListSqlOne(String orderBy,String key,Map<String,Object> dic) {
		JSONObject json=interfaceSignService.getCusfaceListSqlOne(key, orderBy,dic);
		getCusfaceListSqlG(json);
		return json;
	}

	public void getCusfaceListSqlG(JSONObject json){
		JSONArray arrI = new JSONArray();
		if (json.has("rows")) {
			arrI = json.getJSONArray("rows");
			if (arrI.length() > 0) {
				for (int i = 0; i < arrI.length(); i++) {
					JSONObject jo = arrI.getJSONObject(i);
					if (jo.has("FILE_PATH")) {
						jo.put("FILE_PATH",LogicPathUtil.getBrowserPath(jo.getString("STORAGE_ADDRESS_ID"),jo.getString("FILE_PATH")));
					}
					/*if (jo.has("ODEVITY")) {
						int odevity = jo.getInt("ODEVITY");
						if(odevity == OdevityEnum.ODEVITY_ALL.getType()){
							jo.put("ODEVITY",ResourceUtils.getMessage("SYSTEM_WHOLE"));
						}else if(odevity == OdevityEnum.ODEVITY_EVEN.getType()){
							jo.put("ODEVITY",ResourceUtils.getMessage("SYSTEM_EVEN_PAGE"));
						}else if(odevity == OdevityEnum.ODEVITY_QUEER.getType()){
							jo.put("ODEVITY",ResourceUtils.getMessage("SYSTEM_ODD_NUMBERED_PAGE"));
						}else{
							jo.put("ODEVITY","");
						}
					}*/
				}
			}
		}
	}

	public JSONObject queryQifengAu(String userId,Map<String, Object> dic) {
		return interfaceSignService.queryQifengAu(userId,dic);
	}

	public long queryQifengAuTotal(String userId,Map<String, Object> dic) {
		try{
			return interfaceSignService.queryQifengAuTotal(userId,dic);
		}catch (Exception e){
			return 0;
		}
	}

	public List<QfRule> getCusfaceListSqlOne(String orderBy, String key) {
		return interfaceSignService.getCusfaceListSqlOne(key, orderBy);
	}

	public List<QfRule> getCusfaceListSqlUserId(String userId) throws Exception{
		return interfaceSignService.getCusfaceListSqlUserId(userId);
	}

	public List<GReportMerge> getReportMergeListOne(String key) throws Exception{
		return gReportMergeService.getReportMergeListOne(key);
	}

	public List<GReportMerge> getCoverEndListSqlUserId(String userId) throws Exception{
		return gReportMergeService.getCoverEndListSqlUserId(userId);
	}



	public JSONObject getICusRule(String orderBy, String key,Map<String, Object> dic) {
		return interfaceSignService.getICusRule(key, orderBy, dic);
	}
	
	
	/**
	 * 服务器模式下
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public ISign getInterfaceSignById(String id) throws Exception {
		return interfaceSignService.getInterfaceSignById(id);
	}
	
	public FTPObject getFTPObjectById(String id) {
		return interfaceSignService.getFTPObjectById(id);
	}
	
	public String getCusRulePath(String ruleId){
		return interfaceSignService.getCusRulePath(ruleId);
	}
	
	public boolean getICusRuleByName(String name) throws Exception{
		return interfaceSignService.getICusRuleByName(name);
	}
	
	public List<CustomResource> getQfRule() throws Exception{
		return interfaceSignService.getQfRule();
	}
	
	public String getCusfaceDate (JSONObject json,String key,long total) throws Exception{
		// 对查询结果进行重组
		JSONArray arrI = new JSONArray();
		if (json.has("rows")) {
			arrI = json.getJSONArray("rows");
			if (arrI.length() > 0) {
				for (int i = 0; i < arrI.length(); i++) {
					JSONObject jo = arrI.getJSONObject(i);

					if (jo.has("POSITION")) {
						int position = jo.getInt("POSITION");
						
						if(PositionTypeEnum.POSITION_UP.getType()==position){
							jo.put("POSITION", ResourceUtils.getMessage(PositionTypeEnum.POSITION_UP.getResourceName()));
						}else if (PositionTypeEnum.POSITION_LEFT.getType()==position) {
							jo.put("POSITION", ResourceUtils.getMessage(PositionTypeEnum.POSITION_LEFT.getResourceName()));	
						}else if (PositionTypeEnum.POSITION_DOWN.getType()==position) {
							jo.put("POSITION", ResourceUtils.getMessage(PositionTypeEnum.POSITION_DOWN.getResourceName()));	
						}else if (PositionTypeEnum.POSITION_RIGNT.getType()==position) {
							jo.put("POSITION", ResourceUtils.getMessage(PositionTypeEnum.POSITION_RIGNT.getResourceName()));	
						}
						
					}

					if (jo.has("VERTICALAPEAK")) {
						int verPeak = jo.getInt("VERTICALAPEAK");
						
						if(VertPercentEnum.PERCENT_ONE.getType()==verPeak){
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_ONE.getValue());
						}else if (VertPercentEnum.PERCENT_TWO.getType()==verPeak) {
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_TWO.getValue());	
						}else if (VertPercentEnum.PERCENT_THREE.getType()==verPeak) {
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_THREE.getValue());	
						}else if (VertPercentEnum.PERCENT_FOUR.getType()==verPeak) {
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_FOUR.getValue());	
						}else if (VertPercentEnum.PERCENT_FIVE.getType()==verPeak) {
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_FIVE.getValue());	
						}else if (VertPercentEnum.PERCENT_SIX.getType()==verPeak) {
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_SIX.getValue());	
						}else if (VertPercentEnum.PERCENT_SEVEN.getType()==verPeak) {
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_SEVEN.getValue());	
						}else if (VertPercentEnum.PERCENT_EIGHT.getType()==verPeak) {
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_EIGHT.getValue());	
						}else if (VertPercentEnum.PERCENT_NINE.getType()==verPeak) {
							jo.put("VERTICALAPEAK", VertPercentEnum.PERCENT_NINE.getValue());	
						}
						
					}

					
					if (jo.has("ODEVITY")) {
						int odevity = jo.getInt("ODEVITY");
						if(OdevityEnum.ODEVITY_EVEN.getType()==odevity){
							jo.put("ODEVITY", ResourceUtils.getMessage(OdevityEnum.ODEVITY_EVEN.getResourceName()));
						}else if (OdevityEnum.ODEVITY_QUEER.getType()==odevity) {
							jo.put("ODEVITY", ResourceUtils.getMessage(OdevityEnum.ODEVITY_QUEER.getResourceName()));	
						}else if (OdevityEnum.ODEVITY_ALL.getType()==odevity) {
							jo.put("ODEVITY", ResourceUtils.getMessage(OdevityEnum.ODEVITY_ALL.getResourceName()));	
						}
						
					}
					if (jo.has("SYNCOPATE")) {
						int syncopate = jo.getInt("SYNCOPATE");
						if(SyncopateEnum.SYNCOPATE_AVERAGE.getType()==syncopate){
							jo.put("SYNCOPATE", ResourceUtils.getMessage(SyncopateEnum.SYNCOPATE_AVERAGE.getResourceName()));
						}else if (SyncopateEnum.SYNCOPATE_RANDOM.getType()==syncopate) {
							jo.put("SYNCOPATE", ResourceUtils.getMessage(SyncopateEnum.SYNCOPATE_RANDOM.getResourceName()));	
						}else if (SyncopateEnum.SYNCOPATE_SPACIAL.getType()==syncopate) {
							jo.put("SYNCOPATE", ResourceUtils.getMessage(SyncopateEnum.SYNCOPATE_SPACIAL.getResourceName()));	
						}
						
					}

				}
			}
			json.remove("rows");
			json.put("rows", arrI);
			if(total>0){
				json.put("total",total);
			}
		}
		return json.toString();
	}
	
	
	//logService 类
	
	public void toToDownLoad(String homeUr,long d,Result result,HttpServletRequest request) throws Exception{
		//凌晨统计的签名份数
		 String sign_Timer_Log = LogicPathUtil.getSystemUploadPath(StorageFileCacheManage.getSystemUseAddressId()) + "sign.cb";
		logService.toToDownLoad( homeUr, d, result, request,sign_Timer_Log);
	}
	
	
	
	//organizeService 类
    public boolean hasChildOrganize(String departId) {
		
		return organizeService.hasChildOrganize(departId);
	}

	public boolean hasUserInOragnize(String departId) {
		
		return organizeService.hasUserInOragnize(departId);
	}

	public boolean deleteOrganize(String departId) {
		
		return  organizeService.deleteOrganize(departId);
	}

	//强制删除该部门的子部门以及子用户
	public boolean deleteForceOrganize(String departId) {
	
		return organizeService.deleteForceOrganize(departId);
	}
	

	public  DirManager getDirById(String ID)throws Exception{
      
		return dirManagerService.getDirById(ID);
	}
	
	public  DirManager getDirByDirPath(String dirPath) throws Exception{
	
		return dirManagerService.getDirByDirPath(dirPath);
	}
	
	public  List<DirManager> getDirManagers() throws Exception{
		return dirManagerService.getDirManagers();
	}

	/**
	 * 获取目录列表sql
	 * @return
	 */
	public JSONObject getDirList(String orderBy, String key,Map<String,Object> dic) throws Exception{
		return dirManagerService.getDirList(orderBy, key,dic);
	}

	/**
	 * 增加
	 * @param dir
	 * @return
	 */
	public boolean addDir(DirManager dir) {
		return  dirManagerService.addDir(dir);
	}
	
	
	/**
	 * 更新
	 * @param dir
	 * @return
	 */
	public boolean updateDir(DirManager dir) {
		return dirManagerService.updateDir(dir);
	}
	
	
	/*
	 * 获取错误信息
	 */
	public String getErrorMessage(String newPath,String type,String oldPath,int pathType) throws Exception{
		return dirManagerService.getErrorMessage(newPath,type,oldPath,pathType);
	}
	
	
	//StampService 类
	/**
	 * 根据id获取章对象
	 * @return
	 * @throws Exception
	 */
	public GStamp getStampById(String id)throws Exception{
		return stampService.getStampById(id);
		
	}
	
	public String getFirstStampIdByUserId(String userId) {
		return stampService.getFirstStampIdByUserId(userId);
	}

	public  GStamp getStampObjectDataTable() {
		return stampService.getStampObjectDataTable();
	}
	
	/**
	 * 增加章配置
	 * @param g
	 * @return
	 */
	public boolean  addStamp(GStamp g){
		return stampService.addStamp(g);
	}

	/**
	 * 增加章配置
	 * @param g
	 * @return
	 */
	public boolean  addStamp(GStamp g, GWaterPageNumber gWaterPageNumber){
		try{
			boolean flag = stampService.addStamp(g);
			if(flag){
				flag = waterPageNumberService.addWaterPageNumber(gWaterPageNumber);
			}
			return flag;
		}catch (Exception e){
			LoggerHelper.error(e,"");
			return false;
		}
	}

	public GWaterPageNumber queryWaterPageNumberByStampId(String stampId){
	    try{
            return waterPageNumberService.queryWaterPageNumberByStampId(stampId);
        }catch (Exception e){
	        LoggerHelper.error(e,"");
	        return null;
        }
    }

	/**
	 * 更新章配置
	 * @param g
	 * @return
	 */
	public boolean  updateStamp(GStamp g){
		return stampService.updateStamp(g);
	}

	/**
	 * 更新水印
	 * @param g
	 * @return
	 */
	public boolean  updateStamp(GStamp g,GWaterPageNumber gWaterPageNumber){
		try{
			boolean flag = stampService.updateStamp(g);
			if(flag){
				GWaterPageNumber waterPageNumber = waterPageNumberService.queryWaterPageNumberByStampId(gWaterPageNumber.getgStampId());
				if(waterPageNumber!=null && waterPageNumber.getId()!=null){
					flag = waterPageNumberService.updateWaterPageNumber(gWaterPageNumber);
				}else{
					flag = waterPageNumberService.addWaterPageNumber(gWaterPageNumber);
				}
			}
			return flag;
		}catch (Exception e){
			LoggerHelper.error(e,"");
			return false;
		}
	}
	/**
	 * 获取个人章对象
	 * @return
	 * @throws Exception
	 */
	public  GStamp getUserStampObjectByUserID(String userId) throws Exception{
		return stampService.getUserStampObjectByUserID(userId);
	}
	
	
	/**
	 * 获取公章对象
	 * @return
	 * @throws Exception
	 */
	public  GStamp getCorStampObjectByStampId(String stampId) throws Exception{
		return stampService.getCorStampObjectByStampId(stampId);
	}
	
	/**
	 * 获取可选的报告章
	 * @return
	 */
	public  List<GStamp> getSelectCorStamps(String userId) throws Exception{
		return stampService.getSelectCorStamps(userId);
	}
	
	
	
	/**
	 * 获取可选的报告章
	 * @return
	 */
	public  String getSelectCorStampsStrings(String userId) throws Exception{
		return  stampService.getSelectCorStampsStrings(userId);
	}
	/**
	 * 获取用户个人章
	 * @param stampID
	 * @return
	 * @throws Exception
	 */
	public List<GStamp> getUserGStampById(String stampID) throws Exception{
		return stampService.getUserGStampById(stampID);
	}
	
	public List<GStamp>  getCorStampsObjectByUserId(String userId,String corType,String authorization) throws Exception{
		return stampService.getCorStampsObjectByUserId(userId,corType,authorization);
	}
	

	public List<GStamp>  getCorStampsObject(String corType) throws Exception{
		return stampService.getCorStampsObject(corType);
	}
	
	/**
	 * 关联用户公章
	 * @param userId
	 * @param gStampId
	 * @return
	 */
	public boolean addUserCorStamp(String userId,String gStampId,int type){
		
		return stampService.addUserCorStamp(userId, gStampId,type);
	}		
	

	
	/**
	 * 用户公章和用户是否在g_user_cor_stamp表中已经关联
	 * created by dingf
	 * @param userId
	 * @param gStampId
	 * @return
	 */
	public boolean isUserCorStampConnected(String userId,String gStampId){
		return stampService.isUserCorStampConnected(userId, gStampId);
	}
	
	/**
	 * 用户公章和用户是否在g_user_cor_stamp表中已经关联
	 * created by dingf
	 * @param userId
	 * @return
	 */
	public boolean canCorStampDelete(String userId){
		return stampService.canCorStampDelete(userId);
	}
	
	
	public boolean delUserCorStamp(String userId){
	
		return stampService.delUserCorStamp(userId);
	}
	
	public boolean deleteCorStampV1(String gStampId){
		return stampService.deleteCorStampV1(gStampId);
	}
	
	
	public boolean delUserCorStampByUserId(String userId){
	
		return stampService.delUserCorStampByUserId(userId);
	}
	

	//从根据USER_ID从g_stamp表中取出所有记录的ID,COR_STAMP_NAME
	public List<GStamp>  getUserStampDTByUserId(String userId) throws Exception {
		return stampService.getUserStampDTByUserId(userId);
	}
	
	
	/**
	 * 获取可选的用户章
	 * created by dingf
	 * @return
	 */
	public String getSelectedUserStampsStrings(String userId) throws Exception{
	
		return stampService.getSelectedUserStampsStrings(userId);
	}
	
	/*
	 * 根据章名称获取章
	 */
	public GStamp getStampByStampName(String stampName,int type){
		return stampService.getStampByStampName(stampName,type);
	}
	
	/**
	 * 根据用户章名称判断章是否存在
	 * @param userStampName
	 * @return
	 */
	public boolean checkUserStampNameExist(String userStampName,String colFlag,String id)  throws Exception{
		return stampService.checkUserStampNameExist(userStampName, colFlag,id);
	}
	
	/**
	 * 判断是否有多个草稿水印
	 * @param 
	 * @return
	 */
	public boolean checkWarterExist()  throws Exception{
		return stampService.checkWarterExist();
	}
	
	
	
	/**
	 * 获取可选的报告章
	 * @return
	 */
	public String getSelectCorStampsStringsVersion1(String userId) throws Exception{
		return stampService.getSelectCorStampsStringsVersion1(userId);
	}
	

	/**
	 * 获取公司签章列表
	 * @return
	 * @throws Exception 
	 */
	public JSONObject getStampList(String orderBy,String corFlag,Map<String,Object>dic) throws Exception {
		JSONObject jsonObj1 = stampService.getStampList(corFlag,orderBy, dic);
		getStampListG(jsonObj1);
		return jsonObj1 ;
	}

	/**
	 * 获取公司签章列表
	 * @return
	 * @throws Exception
	 */
	public JSONObject getWaterStapList(String orderBy,String corFlag,Map<String,Object>dic) throws Exception {
		JSONObject jsonObj1 = stampService.getWaterStapList(corFlag,orderBy, dic);
		getStampListG(jsonObj1);
		return jsonObj1 ;
	}


	private void getStampListG(JSONObject jsonObj1) throws Exception{
		if(jsonObj1!=null && jsonObj1.length()>0){
			JSONArray jsonObj = jsonObj1.getJSONArray("rows");
			int size = jsonObj.length();
			for(int i = 0;i<size;i++){
				JSONObject temp =jsonObj.getJSONObject(i);
				if(temp.has("ID")){
					String id = temp.getString("ID");
					GWaterPageNumber gWaterPageNumber = waterPageNumberService.queryWaterPageNumberByStampId(id);
					if(gWaterPageNumber!=null && gWaterPageNumber.getId()!=null){
						if(gWaterPageNumber.getRadioFlag().equals(WaterPageNumberEnum.WATER_PAGE_ALL.getCode())){
							//全部
							temp.put("waterPageNumber",ResourceUtils.getMessage("SYSTEM_WHOLE"));
						}else if(gWaterPageNumber.getRadioFlag().equals(WaterPageNumberEnum.WATER_PAGE_OTHER.getCode())){
							String selectFlag = gWaterPageNumber.getSelectFlag();
							if (selectFlag.equals(WaterPageNumberSelectEnum.WATER_SELECT_PAGE.getCode())) {
								//选择页码
								temp.put("waterPageNumber",ResourceUtils.getMessage("ADMIN_SELETC_PAGES_ONE")+":"+gWaterPageNumber.getSelectPageNumber());
							}else if(selectFlag.equals(WaterPageNumberSelectEnum.WATER_REMOVE_PAGES.getCode())){
								//除去该页
								temp.put("waterPageNumber",ResourceUtils.getMessage("ADMIN_REMOVE_THE_PAGES")+":"+gWaterPageNumber.getSelectPageNumber());
							}else if(selectFlag.equals(WaterPageNumberSelectEnum.WATER_REMOVE_FIRST_END_PAGE.getCode())){
								//除去首尾页
								temp.put("waterPageNumber",ResourceUtils.getMessage("ADMIN_REMOVE_FIRST_END_PAGES"));
							}else if(selectFlag.equals(WaterPageNumberSelectEnum.WATER_FIRST_END_PAGE.getCode())){
								temp.put("waterPageNumber",ResourceUtils.getMessage("ADMIN_FIRST_END_PAGES"));
							}
						}
					}
				}
				int sealType = Integer.valueOf(temp.getString("SEAL_TYPE")) ;
				String storageId =temp.getString("STORAGE_ADDRESS_ID");
				temp.put("FILE_PATH",LogicPathUtil.getBrowserPath(storageId,temp.getString("FILE_PATH")));
				if(sealType == 1){
					temp.put("MATCH_NAME1", "");
					String page = temp.getString("STAMP_PAGE");
					if(page.equals("all")){
						temp.put("STAMP_PAGE1", ResourceUtils.getMessage("THIRD_SIGN_STAMP_ALL_PAGE"));
					}else{
						temp.put("STAMP_PAGE1", ResourceUtils.getMessage("THIRD_SIGN_STAMP_STR")+temp.getString("STAMP_PAGE")+ResourceUtils.getMessage("SYSTEM_PAGE_THREE"));
					}
				}else{
					temp.put("MATCH_NAME1",temp.getString("MATCH_NAME"));
					temp.put("STAMP_PAGE1", "");
				}
			}
		}
	}
	
	/**
	 * 获取所有授权签章
	 * @return
	 */
	
	public JSONObject queryAuthorizeStamp(String userId,String corFlag,Map<String,Object> dic) throws Exception{
		return stampService.queryAuthorizeStamp(userId,corFlag,dic);
	}

	public List<GStamp> getMultiInterfaceSigns() {
		
		return stampService.getMultiInterfaceSigns();
	}
	
	
	/**
	 * 获取用户的COR_STAMP_NAME
	 * @param 
	 * @return
	 */
	public String getUserCorStampName(String ID){
		return stampService.getUserCorStampName(ID);
	}


	/**
	 * 根据id获取章对象
	 * @return
	 * @throws Exception
	 */
	public  List<GStamp> getStampsListByIds(Map<String, Object> paramMap)throws Exception{
		List<GStamp>  g_stampList= stampService.getStampsListByIds(paramMap);
		List<GStamp> returnStampList = new ArrayList();
		if(g_stampList !=null && g_stampList.size()>0){
			for(int i=0;i<g_stampList.size();i++){
				GStamp gs = g_stampList.get(i);
//				gs.setFilePath(CebaPathUtil.getProjectContextPath()+gs.getFilePath());
				gs.setFilePath(LogicPathUtil.getBrowserPath(gs.getStorageAddressId(),gs.getFilePath()));
				if(gs.getSealType().equals(SealTypeEnum.KEYWORD_STAMP_FLAG.getType())){
					int[] widHeig=null;
					File file =new File(gs.getFilePath());
					if(file.exists()){
						widHeig= ImageHelper.getZoomImg(file);
						gs.setImgWidth(widHeig[0]+"");
						gs.setImgHeight(widHeig[1]+"");
					}
				}


				returnStampList.add(gs);
			}
			return returnStampList;
		}else{
			return null;
		}
	}
	
	
	/**
	 * 根据id获取骑缝章对象
	 * @return
	 * @throws Exception
	 */
	public  List<GStamp> getStampsQiFengListByIds(Map<String, Object> paramMap)throws Exception{
	   return stampService.getStampsQiFengListByIds(paramMap);
	}

	/**
	 * 获取水印地址
	 * @return
	 */	
	public  GStamp getWatermark(String qianWaterId,String waterType) {
		return stampService.getWatermark(qianWaterId, waterType);
	}
	

	/*
	 * 根据条件获取第一条水印
	 */
	public GStamp getFirstWaterStampByCondition(String condition) {
		return stampService.getFirstWaterStampByCondition(condition);
	}


	/**
	 * 获取水印列表--签名水印和草稿水印
	 * @param orderBy
	 * @param key
	 * @param dic
	 * @return
	 */
	public JSONObject getWatermarkList(String orderBy, String key, Map<String,Object> dic,String WATERMARK_COR_STAMP_FLAG) {
		return stampService.getWatermarkList(orderBy, key,dic,WATERMARK_COR_STAMP_FLAG);
	}
	
	/**
	 * 根据ID删除
	 * @param stampId
	 * @return
	 */
	public boolean delStampById(String stampId){
		return stampService.delStampById(stampId);
	}
	
	/**
	 * 根据查询条件获取章列表
	 * @param condition
	 * @return
	 */
	public List<GStamp> getGStampListByCondition(String condition){
		return stampService.getGStampListByCondition(condition);
	}



	/**
	 * 获取资质章列表
	 * @return
	 */
//	public  String getCredentialStampList(String key ,String orderBy,Map<String,Object> dic) throws Exception{
//		return  stampService.getCredentialStampList(key,orderBy,dic);
//	}
//

	/**
	 * 获取用户印章授权列表
	 */
	public  String getUserDigitalStampList(String key,int limit,int start) throws Exception{
		JSONObject o = stampService.getUserDigitalStampList(key,limit,start);
		JSONArray arrI=new JSONArray();
		if(o.has("rows")){
			arrI = o.getJSONArray("rows");
			if(arrI.length() >0){
				for(int i = 0 ; i<arrI.length() ; i ++){
					JSONObject jo = arrI.getJSONObject(i);
					String coverEndName="";
					if(jo.has("USER_ID")){
					 //获取用户userId 
				     String userId =jo.getString("USER_ID");
						DataTable	dtPic  = stampService.getUserDigitalStampPicList(userId,1);
						String picStr="";//用来拼接图片路径
						if(dtPic !=null && dtPic.getRowCount() >0){
							picStr+="<div>";
							for (int j = 0; j < dtPic.getRowCount(); j++) {
								//获取去图片路径
								String filePicPath = dtPic.getRow(j).getString("FILE_PATH");
								String storageId = dtPic.getRow(j).getString("STORAGE_ADDRESS_ID");
								picStr+="<div style='margin:4px;float:left; border: 1px solid #ccc; width:60px;height:40px; padding:5px;'><img id='"+userId+j+"' src='"+LogicPathUtil.getBrowserPath(storageId,filePicPath)+"' width='60px' height='40px' /></div>";
							}
							picStr+="</div>";
						}

						DataTable	otPic  = stampService.getUserDigitalStampPicList(userId,4);
						String otImg="";//用来拼接图片路径
						if(otPic !=null && otPic.getRowCount() >0){
							otImg+="<div>";
							for (int k = 0; k < otPic.getRowCount(); k++) {
								//获取去图片路径
								String filePicPath = otPic.getRow(k).getString("FILE_PATH");
								String storageId = otPic.getRow(k).getString("STORAGE_ADDRESS_ID");
								otImg+="<div style='margin:4px;float:left; border: 1px solid #ccc; width:60px;height:40px; padding:5px;'><img id='"+userId+k+"' src='"+LogicPathUtil.getBrowserPath(storageId,filePicPath)+"' width='60px' height='40px' /></div>";
							}
							otImg+="</div>";
						}

						DataTable	dtQiPic = interfaceSignService.getUserDigitalStampPicList(userId);
						String picQiStr = "";
						if(dtQiPic !=null && dtQiPic.getRowCount() >0){
							picQiStr+="<div>";
							for (int j = 0; j < dtQiPic.getRowCount(); j++) {
								//获取去图片路径
								DataRow row =  dtPic.getRow(j);
								String filePicPath = dtQiPic.getRow(j).getString("FILE_PATH");
								String storageId = dtQiPic.getRow(j).getString("STORAGE_ADDRESS_ID");
								picQiStr+="<div style='margin:4px;float:left; border: 1px solid #ccc; width:60px;height:40px; padding:5px;'><img id='"+userId+j+"' src='"+ LogicPathUtil.getBrowserPath(storageId,filePicPath)+"' width='60px' height='40px' /></div>";
							}
							picQiStr+="</div>";
						}
				     coverEndName = gReportMergeService.getUserQifengStampPicList(userId);
				     jo.put("picStr", picStr);
				     jo.put("qifengPicStr", picQiStr);
				     jo.put("otImg", otImg);
				     jo.put("coverEndName", coverEndName);
					}
				}
			}
		}
		o.remove("rows");
		o.put("rows", arrI);
		o.put("total",stampService.getUserDigitalStampCount(key,limit,start));
		return o.toString();
	}

	public  String queryMergeAu(String userId) throws Exception{
		return templateWordService.queryMergeAu(userId).toJSON();

	}

	
	/**
	 * 获取所有
	 * @return
	 * @throws Exception
	 */
	public  String getTemplateWordDataTable() throws Exception{
		return templateWordService.getDataTable().toJSON();

	}
	
	
	/**
	 * 新增
	 * @param tw
	 * @return
	 */
	public boolean add(TemplateWord tw){
	  return templateWordService.add(tw);
		
	}
	
	
	/**
	 * 删除
	 * @return
	 */
	 public boolean delete(String id){
		return templateWordService.delete(id);
	}
	
	/**
	 * 修改
	 * @param tw
	 * @return
	 */
	public boolean modify(TemplateWord tw){
		return templateWordService.modify(tw);
	}
	
	public  TemplateWord getTemplateWordById(String ID)throws Exception{
		TemplateWord word = templateWordService.getTemplateWordById(ID);
		word.setWordPath(LogicPathUtil.getSystemUploadPath(word.getStorageAddressId())+word.getWordPath());
		return word;
	}

	public List<TemplateWord> getTemplateWordList() throws Exception{
		DataTable dt =templateWordService.getTemplateWordList();
		List<TemplateWord> list = new ArrayList<TemplateWord>();
		if(dt !=null && dt.getRowCount() >0){
			for(int i = 0 ; i < dt.getRowCount() ; i ++){
				DataRow dr = dt.getRow(i);
				TemplateWord tw = new TemplateWord();
				tw.setId(dr.getString("ID"));
				tw.setWordName(dr.getString("WORD_NAME"));
				//tw.setWordPath(dr.getString("WORD_PATH"));
				tw.setWordGeneratrName(dr.getString("WORD_GENERATE_NAME"));
				tw.setWordVirtualPath(LogicPathUtil.getBrowserPath(tw.getStorageAddressId(),dr.getString("WORD_VIRTUAL_PATH")));
//				String filePath = CebaPathUtil.getPhysicalWebUploadPath();
				tw.setWordPath(LogicPathUtil.getSystemUploadPath(tw.getStorageAddressId())+dr.getString("WORD_VIRTUAL_PATH"));
				list.add(tw);
			}
		}
		return list;
	}
	
	
	//GReportMergeService 类
	
	/**
	 * 根据id获取
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public GReportMerge getReportMergeById(String id) throws Exception{
		return  gReportMergeService.getReportMergeById(id);
		
	}
	
	/*
	 * 根据id删除
	 */
	public boolean deleteReportMergeById(String id) {
		return gReportMergeService.deleteReportMergeById(id);
	}
	
	
	public boolean updateReportMerge(GReportMerge rm) {
		return gReportMergeService.updateReportMerge(rm);
	}
	
	
	public boolean insertReportMerge(GReportMerge rm) {
		return gReportMergeService.insertReportMerge(rm);
	}
	
	public ViewFondreReportMerge getViewFondreReportMerge(String reportMergeId){
		return gReportMergeService.getViewFondreReportMerge(reportMergeId);
	 }
	
	
	/**
	 * 根据名称查找
	 * @param name
	 * @return
	 */
	public boolean checkReportMergeByName(String name,String id) {
		return gReportMergeService.checkReportMergeByName(name,id);
	}
	
	
	
	public String getReportMergeList(String key, Map<String,Object> dic){
		String jsonString =gReportMergeService.getReportMergeList(key,dic);
		JSONObject o = new JSONObject(jsonString);
		JSONArray arr = new JSONArray();
		if (o.has("rows")) {
			arr = o.getJSONArray("rows");
			if (arr.length() > 0) {
				o.put("total",gReportMergeService.getReportMergeCount(key, dic));
			}
		}
		return o.toString();
	}
	
	public String getFilePathByMergeId(String id,String fieldName){
		return  gReportMergeService.getFilePathByMergeId(id,fieldName);
	}
	
	
	public String getFinishMsg() {
		return upgradeService.getFinishMsg();
	}
	
	
	/**
	 * 备份数据库
	 * @param backupPath 数据库备份路径
	 * @return
	 */
	public Boolean backupDatabase(String backupPath){
		String logPath=LogicPathUtil.getPhysicalSqlLogPath(StorageFileCacheManage.getSystemUseAddressId());
		return upgradeService.backupDatabase(backupPath, logPath);
	}

	public MsgMail getBase(){
		return stampService.getBase();
	}
	
	public String deleteDir(String id){
		return stampService.deleteDir(id);
	}
	
	
	public InterfaceSignService getInterfaceSignService() {
		return interfaceSignService;
	}

	public void setInterfaceSignService(InterfaceSignService interfaceSignService) {
		this.interfaceSignService = interfaceSignService;
	}
	

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public OrganizeService getOrganizeService() {
		return organizeService;
	}

	public void setOrganizeService(OrganizeService organizeService) {
		this.organizeService = organizeService;
	}

	public DirManagerService getDirManagerService() {
		return dirManagerService;
	}

	public void setDirManagerService(DirManagerService dirManagerService) {
		this.dirManagerService = dirManagerService;
	}

	public StampService getStampService() {
		return stampService;
	}

	public void setStampService(StampService stampService) {
		this.stampService = stampService;
	}

	public TemplateWordService getTemplateWordService() {
		return templateWordService;
	}

	public void setTemplateWordService(TemplateWordService templateWordService) {
		this.templateWordService = templateWordService;
	}

	public GReportMergeService getgReportMergeService() {
		return gReportMergeService;
	}

	public void setgReportMergeService(GReportMergeService gReportMergeService) {
		this.gReportMergeService = gReportMergeService;
	}

	public UpgradeService getUpgradeService() {
		return upgradeService;
	}

	public void setUpgradeService(UpgradeService upgradeService) {
		this.upgradeService = upgradeService;
	}

	public WaterPageNumberService getWaterPageNumberService() {
		return waterPageNumberService;
	}

	public void setWaterPageNumberService(WaterPageNumberService waterPageNumberService) {
		this.waterPageNumberService = waterPageNumberService;
	}
}
